package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminCouponServiceImpl implements AdminCouponService {
    @Override
    public List<MarketCoupon> list(String page, String limit, String sort, String order, String name, Short type, Short status) {
        SqlSession session= MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(type.toString())){
            criteria.andTypeEqualTo(type);
        }
        if(!StringUtils.isEmpty(status.toString())){
            criteria.andTypeEqualTo(status);
        }
        page = "1";
        limit = "2";
        marketCouponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        //每页10条记录  1页  2页
        //如果使用分页插件，那么我们得到的list是可以强转成一个page对象，该对象内部会包含上述所需要的total、pages、limit、page等属性
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
        session.commit();
        session.close();
        return marketCoupons;
    }
}
